/// <reference path="c:\Users\Administrator\.vscode\extensions\LL\Library\JS\Api.js" /> 

var conf = data.openConfig("./plugins/X/config.json", "json", "{\n\"OP\":[],\n\"Console\":[]}");

let version = '1.1.3'
let author = 'chenjunjie_0125'

function load() {
    log("");
    log("=============================");
    log("加载成功!");
    log("插件版本:" +  version);
    log("插件作者:" + author);
    log("=============================");
    log("");
}

mc.regPlayerCmd("x", "op工具总GUI", (player, args) => {
    let x_fm = mc.newSimpleForm()
    x_fm.setTitle("op工具")
    x_fm.setContent("请选一项：")
    x_fm.addButton("隐身状态")
    x_fm.addButton("游戏模式")
    player.sendForm(x_fm, (pl, id) => {
        if (id != null) {
            switch (id) {
                case 0:
                    x1(player);
                    break;
                case 1:
                    x2(player);
                    break;
            }
        }
    });
}, 1);

function x1(player) {
    let fm1 = mc.newCustomForm()
    fm1.setTitle("隐身")
    fm1.addLabel("隐身种类，滑动选择：")
    fm1.addStepSlider("§l§e<完全隐身>§f------§l§b<普通隐身>§f------§l§c<解除隐身>\n\n§r当前已选择", ["§l§e完全隐身", "§l§b普通隐身", "§l§c解除隐身"])
    player.sendForm(fm1, (player, data) => {
        if (data != null) {
            switch (data[1]) {
                case 0:
                    mc.runcmd("gamerule sendCommandFeedback false");
                    mc.runcmd("gamerule commandblockoutput false");
                    player.runcmd("playanimation @s animation.creeper.swelling move 999999999");
                    player.runcmd("effect @s invisibility 999999999 1 true");
                    player.tell("隐身状态已更改为 §e完全隐身");
                    break;
                case 1:
                    player.runcmd("effect @s invisibility 999999999 1 true");
                    player.tell("隐身状态已更改为 §b普通隐身");
                    break;
                case 2:
                    player.runcmd("playanimation @s animation.creeper.swelling move 1");
                    player.runcmd("effect @s clear");
                    player.tell("隐身状态已更改为 §c解除隐身");
                    break;
            }
        }
    });
}
  
function x2(player) {
    let fm2 = mc.newCustomForm();
    fm2.setTitle("游戏模式");
    fm2.addLabel("游戏模式，滑动选择：");
    fm2.addStepSlider("§l§b<创造>§f------§l§c<生存>§f------§l§a<冒险>§f------§l§e<旁观>\n\n§f当前已选择", ["§l§b创造", "§l§c生存","§l§a冒险","§l§e旁观"])
    player.sendForm(fm2, (player, data) => {
        if (data != null) {
            switch (data[1]) {
                case 0:
                    player.setGameMode(1);
                    player.tell("游戏模式已更改为 §b创造");
                    break;
                case 1:
                    player.setGameMode(0);
                    player.tell("游戏模式已更改为 §c生存");
                    break;
                case 2:
                    player.setGameMode(2);
                    player.tell("游戏模式已更改为 §a冒险");
                    break;
                case 3:
                    player.setGameMode(3);
                    player.tell("游戏模式已更改为 §e旁观");
                    break;
            }
        }
    });
}

function xr() {
    mc.runcmd('ll unload X.js')
    log(`开始重载`);
    mc.runcmd('ll load plugins/X.js')
    log(`重载完毕`);
}

function XTU() {
    network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/ver/Xver.txt", (status, newversion) => {
        if (status == 200) {
            log("开始检查更新")
            if (newversion != version) {
                log("检测到新版本：" + newversion)
                XTD()
            } else {
                log("已为最新版：" + version)
            }
        } else {
            log("无法更新！")
        }
    })
}

function XTD() {
    log("正在建立下载")
    network.httpGet("https://gitee.com/chen-0125/X_P/raw/master/plugins/X.js", (status, newjs) => {
        if (status == 200) {
            log("建立成功，准备下载")
            file.writeTo('.\\plugins\\X.js', newjs.replace(/\r/g, ''));
            log("下载完成，开始安装")
            setTimeout(function () {
                mc.runcmd('ll unload X.js')
                mc.runcmd('ll load plugins/X.js')
            }, 500)
        } else {
            log("无法下载！")
        }
    })
}

mc.listen("onServerStarted", () => {
    mc.regPlayerCmd("x1", "隐身状态", (player, args) => {
    x1(player);
    }, 1);
    mc.regPlayerCmd("x2", "游戏模式", (player, args) => {
    x2(player);
    }, 1);
    mc.regPlayerCmd("xr", "重载X_T", (player, args) => {
        xr(player);
    }, 1);
    mc.regPlayerCmd("xu", "检查更新", (player, args) => {
        XTU(player);
    }, 1);
    mc.regConsoleCmd("xr", "重载X_T", xr)
    mc.regConsoleCmd('xu', "检查更新", XTU)
});

XTU()
setInterval(XTU, 1800000)
setTimeout(load, 100)